﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    class GetBaseDataSummaryAction : GetAction<Params.GetBaseDataSummaryParams,
        Results.GetBaseDataSummaryResult>
    {
        protected override ActionResult<Results.GetBaseDataSummaryResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            Params.GetBaseDataSummaryParams opParams)
        {
            ServiceResult<List<BaseDataSummary>> serviceResult = new BaseDataService().GetLatestSummaryList();

            Results.GetBaseDataSummaryResult actionResult = new Results.GetBaseDataSummaryResult();
            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                for (int index = 0; index < serviceResult.Data.Count; index++)
                {
                    Results.GetBaseDataSummaryResult.Item item = new Results.GetBaseDataSummaryResult.Item();
                    item.Id = serviceResult.Data[index].Id;
                    item.Code = serviceResult.Data[index].Code;
                    item.Caption = serviceResult.Data[index].Caption;
                    item.Changeset = serviceResult.Data[index].Changeset;

                    actionResult.Add(item);
                }
                return new ActionResult<Results.GetBaseDataSummaryResult>(true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<Results.GetBaseDataSummaryResult>(false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }
}
